home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / Syn Text Editor 2.1.0.46 / synsetup-2.1.0.46.exe / {app} / templates / C(++) / OpenGL.c < prev   
C/C++ Source or Header  |  2003-08-13  |  5KB  |  198 lines

  1. /*Description: OpenGL Program|*/
  2. /*
  3.   Created: {$DateTime} by {$UserName}
  4.  
  5.   $Id: OpenGL.c,v 1.1.2.1 2003/08/13 00:38:46 neum Exp $
  6. */
  7.  
  8. /**************************
  9.  * Includes
  10.  *
  11.  **************************/
  12.  
  13. #include <windows.h>
  14. #include <gl/gl.h>
  15.  
  16.  
  17. /**************************
  18.  * Function Declarations
  19.  *
  20.  **************************/
  21.  
  22. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  23. WPARAM wParam, LPARAM lParam);
  24. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
  25. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
  26.  
  27.  
  28. /**************************
  29.  * WinMain
  30.  *
  31.  **************************/
  32.  
  33. int WINAPI WinMain (HINSTANCE hInstance,
  34.                     HINSTANCE hPrevInstance,
  35.                     LPSTR lpCmdLine,
  36.                     int iCmdShow)
  37. {
  38.     WNDCLASS wc;
  39.     HWND hWnd;
  40.     HDC hDC;
  41.     HGLRC hRC;        
  42.     MSG msg;
  43.     BOOL bQuit = FALSE;
  44.     float theta = 0.0f;
  45.  
  46.     /* register window class */
  47.     wc.style = CS_OWNDC;
  48.     wc.lpfnWndProc = WndProc;
  49.     wc.cbClsExtra = 0;
  50.     wc.cbWndExtra = 0;
  51.     wc.hInstance = hInstance;
  52.     wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  53.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  54.     wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
  55.     wc.lpszMenuName = NULL;
  56.     wc.lpszClassName = "GLSample";
  57.     RegisterClass (&wc);
  58.  
  59.     /* create main window */
  60.     hWnd = CreateWindow (
  61.       "GLSample", "OpenGL Sample", 
  62.       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
  63.       0, 0, 256, 256,
  64.       NULL, NULL, hInstance, NULL);
  65.  
  66.     /* enable OpenGL for the window */
  67.     EnableOpenGL (hWnd, &hDC, &hRC);
  68.  
  69.     /* program main loop */
  70.     while (!bQuit)
  71.     {
  72.         /* check for messages */
  73.         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  74.         {
  75.             /* handle or dispatch messages */
  76.             if (msg.message == WM_QUIT)
  77.             {
  78.                 bQuit = TRUE;
  79.             }
  80.             else
  81.             {
  82.                 TranslateMessage (&msg);
  83.                 DispatchMessage (&msg);
  84.             }
  85.         }
  86.         else
  87.         {
  88.             /* OpenGL animation code goes here */
  89.  
  90.             glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
  91.             glClear (GL_COLOR_BUFFER_BIT);
  92.  
  93.             glPushMatrix ();
  94.             glRotatef (theta, 0.0f, 0.0f, 1.0f);
  95.             glBegin (GL_TRIANGLES);
  96.             glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
  97.             glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
  98.             glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
  99.             glEnd ();
  100.             glPopMatrix ();
  101.  
  102.             SwapBuffers (hDC);
  103.  
  104.             theta += 1.0f;
  105.             Sleep (1);
  106.         }
  107.     }
  108.  
  109.     /* shutdown OpenGL */
  110.     DisableOpenGL (hWnd, hDC, hRC);
  111.  
  112.     /* destroy the window explicitly */
  113.     DestroyWindow (hWnd);
  114.  
  115.     return msg.wParam;
  116. }
  117.  
  118.  
  119. /********************
  120.  * Window Procedure
  121.  *
  122.  ********************/
  123.  
  124. LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
  125.                           WPARAM wParam, LPARAM lParam)
  126. {
  127.  
  128.     switch (message)
  129.     {
  130.     case WM_CREATE:
  131.         return 0;
  132.     case WM_CLOSE:
  133.         PostQuitMessage (0);
  134.         return 0;
  135.  
  136.     case WM_DESTROY:
  137.         return 0;
  138.  
  139.     case WM_KEYDOWN:
  140.         switch (wParam)
  141.         {
  142.         case VK_ESCAPE:
  143.             PostQuitMessage(0);
  144.             return 0;
  145.         }
  146.         return 0;
  147.  
  148.     default:
  149.         return DefWindowProc (hWnd, message, wParam, lParam);
  150.     }
  151. }
  152.  
  153.  
  154. /*******************
  155.  * Enable OpenGL
  156.  *
  157.  *******************/
  158.  
  159. void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
  160. {
  161.     PIXELFORMATDESCRIPTOR pfd;
  162.     int iFormat;
  163.  
  164.     /* get the device context (DC) */
  165.     *hDC = GetDC (hWnd);
  166.  
  167.     /* set the pixel format for the DC */
  168.     ZeroMemory (&pfd, sizeof (pfd));
  169.     pfd.nSize = sizeof (pfd);
  170.     pfd.nVersion = 1;
  171.     pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
  172.       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  173.     pfd.iPixelType = PFD_TYPE_RGBA;
  174.     pfd.cColorBits = 24;
  175.     pfd.cDepthBits = 16;
  176.     pfd.iLayerType = PFD_MAIN_PLANE;
  177.     iFormat = ChoosePixelFormat (*hDC, &pfd);
  178.     SetPixelFormat (*hDC, iFormat, &pfd);
  179.  
  180.     /* create and enable the render context (RC) */
  181.     *hRC = wglCreateContext( *hDC );
  182.     wglMakeCurrent( *hDC, *hRC );
  183.  
  184. }
  185.  
  186.  
  187. /******************
  188.  * Disable OpenGL
  189.  *
  190.  ******************/
  191.  
  192. void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
  193. {
  194.     wglMakeCurrent (NULL, NULL);
  195.     wglDeleteContext (hRC);
  196.     ReleaseDC (hWnd, hDC);
  197. }
  198.